/*
* File: CFdEventEpoll.cpp
* Copyright (c) 2010. Silviu Caragea <silviu@intelliproject.net>
* Created on September 15, 2010, 9:15 PM
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*  1.Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
*  2.Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
*  3.All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*       This product includes software developed by the University of
*       California, Berkeley and its contributors.
*  4.Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "CFdEventEpoll.h"

#ifdef USE_EPOLL

namespace litevent
{


int CFdEventEpoll::deleteEvent(int fd_node, int fd )
{    
    if (fd_node < 0)
        return 0;

    struct epoll_event ep;
    
    memset(&ep, 0, sizeof(ep));

    ep.data.fd = fd;
    ep.data.ptr = NULL;

    if (0 != epoll_ctl(this->epollFd, EPOLL_CTL_DEL, fd, &ep))
    {
        util::CLogger::sharedLogger()->WriteFormated("%s.%d: CFdEventEpoll::deleteEvent epoll_ctl failed: %s ", __FILE__, __LINE__,strerror(errno));
        return 0;
    }

    return 1;
}

int CFdEventEpoll::setEvent(int fd_node , int fd, int events)
{
    int add = (fd_node == -1) ? 1 : 0;

    struct epoll_event ep;
    memset(&ep, 0, sizeof(ep));

    ep.events = 0;

    if (events & FDEVENT_IN)
        ep.events |= EPOLLIN;

    if (events & FDEVENT_OUT)
        ep.events |= EPOLLOUT;

    /* with EPOLLET we don't get a FDEVENT_HUP if the close is delay after everything has sent. */

    ep.events |= EPOLLERR | EPOLLHUP /* | EPOLLET */;
    ep.data.ptr = NULL;
    ep.data.fd = fd;

    if (0 != epoll_ctl(this->epollFd, add ? EPOLL_CTL_ADD : EPOLL_CTL_MOD, fd, &ep))
    {
        util::CLogger::sharedLogger()->WriteFormated("%s.%d: CFdEventEpoll::setEvent epoll_ctl failed: %s ", __FILE__, __LINE__,strerror(errno));
        return -1;
    }

    return fd;
}

int CFdEventEpoll::poll(int timeoutMs)
{
    // -1 wait infinite
    //  0 return immediatly
    
    return epoll_wait(this->epollFd, this->epollEvents, this->maxfds, timeoutMs);
}

int CFdEventEpoll::getEventRevent(size_t node)
{
    int events = 0;
    int e = this->epollEvents[node].events;
    
    if (e & EPOLLIN)  events |= FDEVENT_IN;
    if (e & EPOLLOUT) events |= FDEVENT_OUT;
    if (e & EPOLLERR) events |= FDEVENT_ERR;
    if (e & EPOLLHUP) events |= FDEVENT_HUP;
    if (e & EPOLLPRI) events |= FDEVENT_PRI;
    
    return events;
}

int CFdEventEpoll::getEventFileDescriptor(size_t node)
{
    return this->epollEvents[node].data.fd;
}

int CFdEventEpoll::getEventNextFileDescriptor(int node)
{    
    return (node < 0) ? 0 : node + 1;
}

}

#endif
